﻿using UnityEditor;
using UnityEngine;

namespace HNet
{
    public static class EncodeHelper
    {
        public static byte[] Vector3ToBytes(Vector3 v3)
        {
            float[] temp = { v3.x, v3.y, v3.z };
            return ProtocolBuffer.Serialize<float[]>(temp);
        }

        public static Vector3 BytesToVector3(byte[] bytes)
        {
            float[] temp = ProtocolBuffer.DeSerialize<float[]>(bytes);
            return new Vector3(temp[0], temp[1], temp[2]);
        }

#if UNITY_EDITOR

        //用到编辑器的代码
        public static byte[] PrefabToBytesPath(GameObject go)
        {
            if (PrefabUtility.GetPrefabType(go) == PrefabType.Prefab)
            {
                string path = AssetDatabase.GetAssetPath(go);

                return ProtocolBuffer.Serialize<string>(path);
            }

            Debug.LogError(go.name + "不是预设，无法导为地址");
            return null;
        }

        public static GameObject BytesPathToPrefab(byte[] bytes)
        {
            string path = ProtocolBuffer.DeSerialize<string>(bytes);
            GameObject prefab = AssetDatabase.LoadAssetAtPath(path, typeof(GameObject)) as GameObject;
            return prefab;
        }

#endif
    }
}